package org.mozhu.learning.algo.binary_search;

public class BinarySearchV1 implements BinarySearch {

    @Override
    public int binarySearch(int[] a, int value) {
        int low = 0;
        int high = a.length - 1;
        if (a[low] > value || a[high] < value) return -1;

        while (low <= high) {
            int mid = low + ((high - low) >> 1);
            if (a[mid] == value) {
                return mid;
            } else if (a[mid] > value) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
}
